 package handling.channel;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import server.shops.HiredMerchant;

import java.util.*;
import java.util.concurrent.locks.ReentrantReadWriteLock;









 public class MerchantStorage {
/*  12 */   private static final Logger log = LogManager.getLogger(ChannelServer.class);
   private final int channel;
/*  14 */   private final Map<Integer, HiredMerchant> merchants = new HashMap<>();
/*  15 */   private int running_MerchantID = 0;
/*  16 */   private ReentrantReadWriteLock merchLock = null;
/*  17 */   private ReentrantReadWriteLock.ReadLock mcReadLock = null;
/*  18 */   private ReentrantReadWriteLock.WriteLock mcWriteLock = null;

   MerchantStorage(int channel) {
/*  21 */     this.channel = channel;

/*  23 */     this.merchLock = new ReentrantReadWriteLock(true);
/*  24 */     this.mcReadLock = this.merchLock.readLock();
/*  25 */     this.mcWriteLock = this.merchLock.writeLock();
   }

   public void closeAllMerchants() {
/*  29 */     int ret = 0;
/*  30 */     long Start = System.currentTimeMillis();
/*  31 */     this.mcWriteLock.lock();
     try {
/*  33 */       Iterator<Map.Entry<Integer, HiredMerchant>> hmit = this.merchants.entrySet().iterator();
/*  34 */       while (hmit.hasNext()) {
/*  35 */         ((HiredMerchant)((Map.Entry)hmit.next()).getValue()).closeShop(true, false);
/*  36 */         hmit.remove();
/*  37 */         ret++;
       }
/*  39 */     } catch (Exception e) {
/*  40 */       log.error("关闭雇佣商店出现错误..." + e);
     } finally {
/*  42 */       this.mcWriteLock.unlock();
     }
/*  44 */     log.info("频道 " + this.channel + " 共保存雇佣商店: " + ret + " | 耗时: " + (System.currentTimeMillis() - Start) + " 毫秒.");
   }

   public int addMerchant(HiredMerchant hMerchant) {
/*  48 */     this.mcWriteLock.lock();
     try {
/*  50 */       this.running_MerchantID++;
/*  51 */       this.merchants.put(Integer.valueOf(this.running_MerchantID), hMerchant);
/*  52 */       return this.running_MerchantID;
     } finally {
/*  54 */       this.mcWriteLock.unlock();
     }
   }

   public void removeMerchant(HiredMerchant hMerchant) {
/*  59 */     this.mcWriteLock.lock();
     try {
/*  61 */       this.merchants.remove(Integer.valueOf(hMerchant.getStoreId()));
     } finally {
/*  63 */       this.mcWriteLock.unlock();
     }
   }

   public boolean containsMerchant(int accId) {
/*  68 */     boolean contains = false;
/*  69 */     this.mcReadLock.lock();
     try {
/*  71 */       for (HiredMerchant hm : this.merchants.values()) {
/*  72 */         if (hm.getOwnerAccId() == accId) {
/*  73 */           contains = true;
           break;
         }
       }
     } finally {
/*  78 */       this.mcReadLock.unlock();
     }
/*  80 */     return contains;
   }

   public boolean containsMerchant(int accId, int chrId) {
/*  84 */     boolean contains = false;
/*  85 */     this.mcReadLock.lock();
     try {
/*  87 */       for (HiredMerchant hm : this.merchants.values()) {
/*  88 */         if (hm.getOwnerAccId() == accId && hm.getOwnerId() == chrId) {
/*  89 */           contains = true;
           break;
         }
       }
     } finally {
/*  94 */       this.mcReadLock.unlock();
     }
/*  96 */     return contains;
   }

   public List<HiredMerchant> searchMerchant(int itemSearch) {
/* 100 */     List<HiredMerchant> list = new LinkedList<>();
/* 101 */     this.mcReadLock.lock();
     try {
/* 103 */       for (HiredMerchant hm : this.merchants.values()) {
/* 104 */         if (hm.searchItem(itemSearch).size() > 0) {
/* 105 */           list.add(hm);
         }
       }
     } finally {
/* 109 */       this.mcReadLock.unlock();
     }
/* 111 */     return list;
   }

   public HiredMerchant getHiredMerchants(int accId, int chrId) {
/* 115 */     this.mcReadLock.lock();
     try {
/* 117 */       for (HiredMerchant hm : this.merchants.values()) {
/* 118 */         if (hm.getOwnerAccId() == accId && hm.getOwnerId() == chrId) {
/* 119 */           return hm;
         }
       }
     } finally {
/* 123 */       this.mcReadLock.unlock();
     }
/* 125 */     return null;
   }
 }


/* Location:              C:\PGX\Server.jar!\handling\channel\MerchantStorage.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */